
//获取某个localstorage值
export function get(name) {
    let value = localStorage.getItem(name);
    if (!value) {
    return null;
    }
    try {
    value = JSON.parse(value);
    } catch (e) {
    console.error(e);
    }
    return value;
}

//设置localstorage
export function set(name, value) {
    localStorage.setItem(name, JSON.stringify(value));
}
//清空localstorage
export function clear(){
    localStorage.clear();
}
//删除某个localstorage
export function remove(name) {
    localStorage.removeItem(name);
}
//获取session
export function getSessionStorage(name){
    let value = sessionStorage.getItem(name);
    if (!value) {
    return null;
    }
    try {
    value = JSON.parse(value);
    } catch (e) {
    console.error(e);
    }
    return value;
}
//设置session
export function setSessionStorage(name, value) {
    sessionStorage.setItem(name, JSON.stringify(value));
}
export function clearSessionStorage(){
    sessionStorage.clear();
}
export function serialize(obj) {
    let query = '',
    // name,
    value,
    fullSubName,
    // subName,
    subValue,
    innerObj,
    i;

    for (const name in obj) {
    value = obj[name];

    if (value instanceof Array) {
        for (i = 0; i < value.length; ++i) {
        subValue = value[i];
        fullSubName = name + '[' + i + ']';
        innerObj = {};
        innerObj[fullSubName] = subValue;
        query += this.serialize(innerObj) + '&';
        }
    } else if (value instanceof Object) {
        for (const subName in value) {
        subValue = value[subName];
        fullSubName = name + '[' + subName + ']';
        innerObj = {};
        innerObj[fullSubName] = subValue;
        query += this.serialize(innerObj) + '&';
        }
    } else if (value !== undefined && value !== null) {
        query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
    }
    }

    return query.length ? query.substr(0, query.length - 1) : query;
}
//设置cookie
export function setCookie(name, value,expiredays){
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = name + "=" + escape(value) + ((expiredays == null) ? "" : "^;expires=" + exdate.toUTCString());
    console.log(document.cookie)
}
export function getCookieVal(offset) {
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1) endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}
//获取cookie
export function getCookie(name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
    var j = i + alen;
    //alert(j);
    if (document.cookie.substring(i, j) == arg) return getCookieVal(j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
    }
    return null;
}
//删除cookie
export function deleteCookie(name){
    var date=new Date();
    date.setTime(date.getTime()-10000);
    document.cookie=name+"=v; expires="+date.toUTCString();
}
//获取缓存中id对应的name
export function getAA10NamebyCode(type,code){
    let aa10List = getSessionStorage("AA10_"+type);
    if(aa10List && aa10List.length){
    for(let i=0;i<aa10List.length;i++){
        let aa10 = aa10List[i];
        if(aa10['key'] == code){
        return aa10['title'];
        }
    }
    }
    return "";
}
/**
 * 根据字典值code获取字典名称name
 * @param  aaa102 字典code
 * @param  dict 字典大类
 */
export function getAAA103byaaa102(aaa102,dict){
    let aaa103='';
    let dictArr=getSessionStorage('AA10_'+dict);
    if(dictArr && dictArr.length){
        dictArr.forEach(item => {
            if(item.value==aaa102){
                aaa103=item.name;
                return aaa103;
            }
        });
    }
    return aaa103;
}